package com.team12.hotel404.controller.admin;

import com.team12.hotel404.domain.Power;
import com.team12.hotel404.service.PowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping(value = "power")
public class PowerController {
    @Autowired
    private PowerService powerService;

    @RequestMapping(value = "list")
    public String list_all_power(Model model){
        List<Power> powers = powerService.listAllPower();
        model.addAttribute("powers",powers);
        return "admin/power-list";
    }
    @RequestMapping(value = "/insert", method = RequestMethod.GET)
    public String insert_power() {
        return "admin/power-insert";
    }

    @RequestMapping(value = "insert")
    public String insert_power(Power power){
        powerService.insertPower(power);
        return "redirect:/power/list";
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update_power(Power power){
        powerService.updatePowerInfo(power);
        return "redirect:/power/list";
    }

    @RequestMapping(value = "/findById")
    public String find_power_by_id(Integer id, Model model) {
        Power power = powerService.findById(id);
        model.addAttribute("power", power);
        return "/admin/power-update";
//        return "admin/powerTest";
    }

    @RequestMapping(value = "delete")
    public String delete_power(Integer id){
        powerService.deletePower(id);
        return "redirect:/power/list";
    }
    @RequestMapping("/del")
    public String del_power(Integer id) {
        powerService.deletePowerById(id);
        return "redirect:/power/list";
    }
}
